//
//  GJFIntro.m
//  Wedding
//
//  Created by 郭静芬 on 2017/1/13.
//  Copyright © 2017年 郭静芬. All rights reserved.
//

#import "GJFIntro.h"
#import "GJFIntroCollectionViewCell.h"
#import "GJFHomeViewController.h"

@interface GJFIntro () <UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIImageView *imageBack;
@property (nonatomic, strong) NSArray *imageArr;
@property (nonatomic, strong) UIButton *btnIntro;
@property (nonatomic, assign) BOOL is_first;
@end

@implementation GJFIntro

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        [self.imageBack mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.mas_equalTo(self).mas_offset(0);
            make.height.mas_offset(HEIGHT / 3 * 2 + 10 * YY);
        }];
        [self collectionView];
        int width = 20 * XX;
        int space = 10 * XX;
        int startX = WIDTH / 2 - width - width / 2 - space;
        for (NSInteger i = 0; i < 3; i++) {
            UILabel *lbl = [[UILabel alloc] init];
            lbl.frame = CGRectMake(startX + (i * (width + space)), HEIGHT / 3 * 2 + 100 * YY, width, 3 * YY);
            lbl.tag = 1010 + i;
            [self addSubview:lbl];
        }
        [self changeLblStatus:0];
        [self.btnIntro mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).mas_offset(WIDTH / 4);
            make.right.mas_equalTo(self.mas_right).mas_offset(-WIDTH / 4);
            make.bottom.mas_equalTo(self.mas_bottom).mas_offset(- 50 * YY);
            make.height.mas_offset(30 * YY);
        }];
        
    }
    return self;
}
- (void)clickToHome {
    
    [UIView animateWithDuration:1 animations:^{
        self.frame = CGRectMake(-WIDTH, 0, WIDTH, HEIGHT);
    } completion:^(BOOL finished) {
        self.hidden = YES;
        [self removeFromSuperview];
        [[UIApplication sharedApplication] setStatusBarHidden:NO];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"introFirst"];
    }];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / WIDTH;
    [self changeLblStatus:index];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x * 1.0 / WIDTH > 2.2 && !_is_first) {
        [self clickToHome];
        _is_first = YES;
    }
}

- (void)changeLblStatus:(NSInteger)index {
    
    for (NSInteger i = 0; i < 3; i++) {
        UILabel *label = [self viewWithTag:1010 + i];
        if (i == index) {
            label.backgroundColor = HOMECOLOR;
        }
        else {
            label.backgroundColor = [UIColor lightGrayColor];
        }
            
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GJFIntroCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"IntroCell" forIndexPath:indexPath];
    cell.imageV.image = [UIImage imageNamed:[self.imageArr[indexPath.item] objectForKey:@"img"]];
    cell.label.text = [self.imageArr[indexPath.item] objectForKey:@"title"];
    return cell;
}
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(WIDTH, HEIGHT);
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.minimumLineSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[GJFIntroCollectionViewCell class] forCellWithReuseIdentifier:@"IntroCell"];
        [self addSubview: _collectionView];
    }
    return _collectionView;
}
- (UIImageView *)imageBack {
    if (!_imageBack) {
        _imageBack = [[UIImageView alloc] init];
        _imageBack.image = [UIImage imageNamed:@"mineheaderImg"];
        UIBlurEffect *b = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        UIVisualEffectView *bV = [[UIVisualEffectView alloc] initWithEffect:b];
        bV.frame = _imageBack.bounds;
        [_imageBack addSubview:bV];
        [bV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        [self addSubview:_imageBack];
    }
    return _imageBack;
}
- (NSArray *)imageArr {
    if (!_imageArr) {
        _imageArr = @[@{@"img":@"1", @"title":@"千万新人备婚经验供你参考"}, @{@"img":@"2", @"title":@"海量靠谱婚礼商家任你选购"}, @{@"img":@"3", @"title":@"多张精美婚礼图片免费收藏"}];
    }
    return _imageArr;
}
- (UIButton *)btnIntro {
    if (!_btnIntro) {
        _btnIntro = [[UIButton alloc] init];
        [_btnIntro setTitle:@"开启幻紫琉晶" forState:UIControlStateNormal];
        [_btnIntro setBackgroundColor:HOMECOLOR];
        [_btnIntro setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _btnIntro.layer.cornerRadius = 15 * YY;
        _btnIntro.clipsToBounds = YES;
        _btnIntro.titleLabel.font = [UIFont systemFontOfSize:15 * XX];
        [_btnIntro addTarget:self action:@selector(clickToHome) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_btnIntro];
    }
    return _btnIntro;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
